﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ZebraCoreLib.Notice;
using System.Web.Mvc;
using System.IO;
using Web.ZebraUIControls;
using System.Text;

namespace System.Web.Mvc.Html
{

    
    public static class HtmlHelperExtented
    {

        public static void RenderUserControls(HtmlHelper html,string userControls,object model)
        {
            html.RenderPartial("/Views/Shared/UserControls/" + userControls ,null,new ViewDataDictionary(model));
        }
        public static HtmlTextWriter GetTextWriter(HtmlHelper html)
        {
            HtmlTextWriter writer = new HtmlTextWriter(html.ViewContext.HttpContext.Response.Output);
           
            return writer;
        }
        public static void RenderDataField(this HtmlHelper html,string dataField)
        {
            ZebraTextBox text = new ZebraTextBox();
            var writer=GetTextWriter(html);
            text.RenderControl(writer);
        }

        public static void RenderQueryZone(this HtmlHelper html, ZebraQueryZone zone)
        {
            var writer = GetTextWriter(html);
            zone.BizObjectType = GetBizObjectType(html);
            zone.html = html;
            zone.RenderControl(writer);
             
           
            //if (html.ViewData["ScriptsContainer"] == null)
            //    html.ViewData["ScriptsContainer"] = new StringBuilder();
            //(html.ViewData["ScriptsContainer"] as StringBuilder).Append(zone.RenderControlScript());
            
            //writer.Write((html.ViewData["ScriptsContainer"] as StringBuilder).ToString());
        }

        public static void RenderListZone(this HtmlHelper html ,ZebraListZone zone)
        {
            var writer = GetTextWriter(html);
            zone.BizObjectType = GetBizObjectType(html);
            zone.html = html;
            zone.RenderControl(writer);
            if (html.ViewData["ScriptsContainer"] == null)
                html.ViewData["ScriptsContainer"] = new StringBuilder();
            (html.ViewData["ScriptsContainer"] as StringBuilder).Append(zone.RenderControlScript());
        }

        public static void RenderPageTitle(this HtmlHelper html, ZebraPageTitle title)
        {
            var writer = GetTextWriter(html);
            title.BizObjectType = GetBizObjectType(html);
            title.RenderControl(writer);
        }

        public static void RenderScripts(this HtmlHelper html)
        {
            if (html.ViewData["ScriptsContainer"] != null)
            {
                var writer = GetTextWriter(html);

                writer.Write("js beging[");
                writer.Write((html.ViewData["ScriptsContainer"] as StringBuilder).ToString());
                writer.Write("]js end");
            }
        }

        public static Type GetBizObjectType(HtmlHelper html)
        {
            Type resultType = null;
            Type viewDataType= html.ViewData.GetType();
            if (null != viewDataType)
            {
                var argTypes= viewDataType.GetGenericArguments();
                if (null != argTypes && argTypes.Length >= 1)
                {
                    resultType = argTypes[0];
                }
            }
            return resultType;
        }
    }
}
